home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9364 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  84 lines

  1. Path: nntp.onyx.net!claymoor
  2. From: Adam.Morris@octacon.co.uk (Adam Morris)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [HLP] Easy Problem w/ Classes!
  5. Date: Fri, 01 Mar 96 14:52:35 GMT
  6. Organization: Octacon Ltd
  7. Message-ID: <4h6hea$3v5@mulgave.octacon.co.uk>
  8. References: <4gfn3v$jp@news.umbc.edu>
  9. NNTP-Posting-Host: claymoor.onyx.net
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <4gfn3v$jp@news.umbc.edu>, ssopre1@umbc.edu (Sunil ) wrote:
  13. >Hello all
  14. >  Heres a little problem that I need help with. 
  15. >
  16. >// Point.h contains
  17. >Class Point {
  18. >private: int x;
  19. >     int y;
  20. >
  21. >public: Point();
  22. >}
  23. >
  24. >// LineSeg.h contains 
  25. >class LineSeg{
  26. >private:
  27. >    Point end;
  28. >    Point start;
  29. >public:        
  30. >    LineSeg();
  31. >}
  32. >
  33. >// LineSeg.C contains
  34. >LineSeg::LineSeg()
  35. >{
  36. > Point.end.x=0;  // Are these valid? 
  37. > Point.end.y=0;  // What do I need to add/del here? 
  38. > Point.start.x=0;
  39. > Point.start.y=0;
  40. >}
  41. >
  42. >Basically, LineSeg() is supposed to set starting and ending
  43. >points to zero. Compiler goes berserkoid .. . 
  44.  
  45. I'm not surprised, you are attempting to access private data members of point.
  46.  
  47. you either need to make them public (in which case just replace point with a 
  48. struct) or more sensibly add functions to return x and y (try setting them as 
  49. well)
  50.  
  51. you should end up with 
  52. class Point{
  53.  public:
  54.   Point();
  55.   Point(int x, int y);
  56.  
  57.   void setPoint(int anXValue, int anYValue);
  58.   void setX(int anXValue);
  59.   void setY(int aYValue);
  60.  
  61.   int getX();
  62.   int getY();
  63.  private:
  64.   int theXValue;
  65.   int theYValue;
  66. };
  67.  
  68. I'll leave you to write the bodies...  (this is incomplete you would probably 
  69. want a copy constructor and an operator= as well)
  70.  
  71. Then in line seg you can declare your points
  72.  
  73. Point end(1, 2);
  74. Point start();
  75.  
  76. start.set(3, 4);
  77.  
  78. int aValue;
  79. aValue = end.getX();
  80. ..
  81. and so on.
  82.  
  83. Adam
  84.